package com.ruoyi.eam.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.eam.complex.LoanBackDeptName;
import com.ruoyi.eam.complex.UpdateEamChg;
import com.ruoyi.eam.complex.UpdateEamLoanBack;
import com.ruoyi.eam.doman.*;
import com.ruoyi.eam.service.*;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 借用归还Controller
 *
 * @author xl
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/loanBack")
public class EamLoanbackController extends BaseController
{
    @Autowired
    private IEamLoanbackHService eamLoanbackHService;
    @Autowired
    private IEamLocationService locationService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IEamTypeService eamTypeService;
    @Autowired
    private IEamLoanbackDService eamLoanbackDService;
    @Autowired
    private IEamAssetService eamAssetService;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 查询借用归还列表
     */
    //@RequiresPermissions("eam:loanBack:list")
    @GetMapping("/list")
    public TableDataInfo list(EamLoanbackH eamLoanbackH)
    {
        startPage();
        List<EamLoanbackH> list = eamLoanbackHService.selectEamLoanbackHList(eamLoanbackH);
        return getDataTable(list);
    }

    /**
     * 查询资产类别,存放位置,部门列表
     */
    //@RequiresPermissions("system:loanBack:typeList")
    @GetMapping("/typeList")
    public Map<String,Object> typeList(EamChgH eamChgH)
    {
        Map<String,Object> rm = new HashMap<>();
        startPage();
        List<EamType> typeList = eamTypeService.selectEamTypeList(null);
        List<EamLocation> locations = locationService.selectEamLocationList(null);
        List<Dept> depts = deptService.selectDeptList(null);
        rm.put("typeList",typeList);
        rm.put("locationList",locations);
        rm.put("deptList",depts);
        return rm;
    }

    /**
     * 查询资产类别,存放位置,部门列表
     */
    //@RequiresPermissions("system:loanBack:deptByUserId")
    @PostMapping("/deptByUserId")
    public Map<String,Object> deptByUserId(@RequestBody LoanBackDeptName loanBackDeptName)
    {
        System.out.println("loanBackDeptName:"+loanBackDeptName);
        Map<String,Object> rm = new HashMap<>();
        User u1 = userService.selectUserByUserId(loanBackDeptName.getCreatorId());
        rm.put("creatorDeptName",deptService.selectDeptByDeptId(u1.getDeptId()).getDeptName());
        if(loanBackDeptName.getAuditId() != null){
            User u2 = userService.selectUserByUserId(loanBackDeptName.getAuditId());
            rm.put("auditDeptName",deptService.selectDeptByDeptId(u2.getDeptId()).getDeptName());
        }
        return rm;
    }

    /**
     * 查询资产类别,存放位置,部门列表
     */
    //@RequiresPermissions("eam:loanBack:userListByDeptId")
    @GetMapping("/userListByDeptId")
    public Map<String,Object> userListByDeptId(User user)
    {
        Map<String,Object> rm = new HashMap<>();
        List<User> userList = userService.selectUserList(user);
        rm.put("userList",userList);
        return rm;
    }

    /**
     * 导出借用归还列表
     */
    //@RequiresPermissions("eam:loanBack:export")
    @Log(title = "借用归还", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamLoanbackH eamLoanbackH)
    {
        List<EamLoanbackH> list = eamLoanbackHService.selectEamLoanbackHList(eamLoanbackH);
        ExcelUtil<EamLoanbackH> util = new ExcelUtil<EamLoanbackH>(EamLoanbackH.class);
        util.exportExcel(response, list, "借用归还数据");
    }

    /**
     * 获取借用归还详细信息
     */
    //@RequiresPermissions("eam:loanBack:query")
    @GetMapping(value = "/{loanBackId}")
    public AjaxResult getInfo(@PathVariable("loanBackId") Long loanBackId)
    {
        return success(eamLoanbackHService.selectEamLoanbackHByLoanBackId(loanBackId));
    }

    /**
     * 新增借用归还
     */
    //@RequiresPermissions("eam:loanBack:add")
    @Log(title = "借用归还", businessType = BusinessType.INSERT)
    @PostMapping("/addH")
    public AjaxResult addH(@RequestBody EamLoanbackH eamLoanbackH)
    {
        //锁
        RLock lock = redissonClient.getLock("myinsertLoanCode");
        try{
            if (!lock.tryLock()) {
                return AjaxResult.error("操作过于频繁");
            }else{
                return toAjax(eamLoanbackHService.insertEamLoanbackH(eamLoanbackH));
            }
        }finally {
            //释放
            lock.unlock();
        }
    }

    /**
     * 新增资产变更从表
     */
    //@RequiresPermissions("eam:loanBack:addD")
    @Log(title = "资产变更", businessType = BusinessType.INSERT)
    @PostMapping("/addD")
    public AjaxResult addD(@RequestBody List<EamAsset> eamAssets)
    {
        //锁
        RLock lock = redissonClient.getLock("myinsertLoanDCode");
        try{
            if (!lock.tryLock()) {
                return AjaxResult.error("操作过于频繁");
            }else{
                return toAjax(eamLoanbackDService.insertEamLoanbackD(eamAssets));
            }
        }finally {
            //释放
            lock.unlock();
        }
    }

    /**
     * 修改借用归还
     */
    //@RequiresPermissions("eam:loanBack:edit")
    @Log(title = "借用归还", businessType = BusinessType.UPDATE)
    @PutMapping("editH")
    public AjaxResult edit(@RequestBody EamLoanbackH eamLoanbackH)
    {
        return toAjax(eamLoanbackHService.updateEamLoanbackH(eamLoanbackH));
    }

    /**
     * 删除借用归还
     */
    //@RequiresPermissions("eam:loanBack:remove")
    @Log(title = "借用归还", businessType = BusinessType.DELETE)
	@DeleteMapping
    public AjaxResult remove(@RequestBody EamLoanbackH eamLoanbackH)
    {
        return toAjax(eamLoanbackHService.deleteEamLoanbackHByLoanBackId(eamLoanbackH));
    }

    /**
     * 修改资产变更
     */
    //@RequiresPermissions("eam:h:editD")
    @Log(title = "修改:删除借用归还从表", businessType = BusinessType.UPDATE)
    @PostMapping("/editD")
    public AjaxResult editD(@RequestBody UpdateEamLoanBack eamLoanBack)
    {
        Long id = eamLoanBack.getEamLoanbackH().getLoanBackId();
        //先做删除从表操作
        int num = eamLoanbackDService.deleteEamLoanbackDByLoanBackId(id);
        //在做添加从表操作
        eamLoanbackDService.updateLoanBackD(eamLoanBack.getEamAssets(),eamLoanBack.getEamLoanbackH().getLoanBackId());
        //修改主
        return toAjax(eamLoanbackHService.updateEamLoanbackH(eamLoanBack.getEamLoanbackH()));
    }

    @Log(title = "修改:资产", businessType = BusinessType.UPDATE)
    @PutMapping("/updateAsset")
    public AjaxResult updateAsset(@RequestBody List<EamAsset> eamAssets)
    {

        int num = 0;
        for (EamAsset eamAsset : eamAssets) {
            num = eamAssetService.updateEamAsset(eamAsset);
        }
        return toAjax(num);
    }

}
